package fms.webservice.server.parser;

import fms.webservice.server.dao.DataReaderDao;
import fms.webservice.server.util.BuildSqlUtil;
import fms.webservice.server.util.XmlUtil;
import fms.webservice.server.common.*;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: root
 * Date: 14-7-9
 * Time: 上午9:27
 * To change this template use File | Settings | File Templates.
 */
public class WindRequestParser implements IRequestParser  {
    private static Logger logger = Logger.getLogger(WindRequestParser.class);

    public Response parser(Request windRequest) {
        Response response = new Response();
        response.setServiceType(windRequest.getServiceType());
        response.setVersion(windRequest.getVersion());
        WindResponseXML windResponseXML = new WindResponseXML();
        WindRequestXML windRequestXML = XmlUtil.unMarshallWindRequest(windRequest.getRequestXML());
        ErrorLevel el = new ErrorLevel();
        try {
            DataReaderDao dr = null;
            try {
                dr = new DataReaderDao();
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                windResponseXML.setResultcode(el.DBError);
                logger.error(e1.getMessage());
                return response;
            }

            //验证请求数据的用户合法性
//            if (!dr.validate(BuildSqlUtil.buildValidateSql(reqmsg.getUserName(), reqmsg.getPassWord()))) {
//                logger.info("error vallidate username or password...");
//                reinfo.setResultcode(el.NoAuthent);
//                return reinfo;
//            }

            String sql = BuildSqlUtil.buildSQL(windRequestXML);
            List li = null;
            try {
                li = dr.getRawMsgList(sql);
            } catch (ClassNotFoundException e) {
                logger.error(e.getMessage());
                windResponseXML.setResultcode(el.DBError);
                return response;
            } catch (IOException e) {
                logger.error(e.getMessage());
                windResponseXML.setResultcode(el.DBError);
                return response;
            }
            Iterator it = li.iterator();
            List<FMSData> listdata = new ArrayList<FMSData>();
            while (it.hasNext()) {
                FMSData dd = new FMSData();
                Map hm = (Map) it.next();
                // 核心模块处理的高度层数据单位比数据库中的小100倍
                dd.setAlt(hm.get("ALT").toString().substring(0, hm.get("ALT").toString().length() - 2));
                dd.setCptname(hm.get("CPT_NAME").toString());
                dd.setWinddir(hm.get("WIND_DIR").toString());
                dd.setWindtep(hm.get("WIND_TEP").toString());
                dd.setWindvel(hm.get("WIND_VEL").toString());

                listdata.add(dd);
            }
            windResponseXML.setResultcode(el.Success);
            if(listdata.size()==0)
            {
                // 这种假数据必须要填充进去，否则XML序列化会异常的
                FMSData dd = new FMSData();
                dd.setAlt( windRequestXML.getAlt() );
                dd.setCptname( windRequestXML.getCptname() );
                dd.setWinddir("0");
                dd.setWindtep("0");
                dd.setWindvel("0");
                listdata.add(dd);
                windResponseXML.setResultcode(ErrorLevel.InquiryError);
            }
            windResponseXML.setCounts(li.size());
            windResponseXML.setDatas(listdata);
        } catch (SQLException e) {
            windResponseXML.setResultcode(el.InquiryError);
            logger.error(e.getMessage());
            return response;
        }
        response.setResponseXML(XmlUtil.marshallWindResponse(windResponseXML));
        logger.info(response.getResponseXML());
        return response;
    }
}
